import { STORAGE_KEY } from "./constant";
export const timestampToDate = (timestamp) => {
  if (!timestamp) return "";
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};

export function buildGetUrl(params) {
  if (!params || Object.keys(params).length === 0) return "";
  //   console.log("params", params);
  //   删除params中没有值的属性
  Object.keys(params).forEach((key) => {
    if (!params[key] && params[key] !== 0) delete params[key];
  });
  //   拼接字符串
  let queryString = Object.keys(params)
    .map((key) => `${key}=${params[key]}`)
    .join("&");

  return `${queryString}`;
}

// 判断按钮权限、
export function hasPermission(value) {
  const all_permission = "*:*:*";
  const permissions = uni.getStorageSync(STORAGE_KEY.userInfo).permissions;

  if (value && value instanceof Array && value.length > 0) {
    const permissionFlag = value;

    const hasPermissions = permissions.some((permission) => {
      return (
        all_permission === permission || permissionFlag.includes(permission)
      );
    });

    if (!hasPermissions) {
      return false;
    } else {
      return true;
    }
  } else {
    throw new Error(t("permission.hasPermission"));
  }
}
